<?php

    /**
    * Update logs
    *  May 31, 2011 - trungdt - First create
    * 
    */

    class rsnewsmodule {
        function name() { return exponent_lang_loadKey('modules/rsnewsmodule/class.php','module_name'); }
        function author() { return 'ABSoft Company'; }
        function description() { return exponent_lang_loadKey('modules/rsnewsmodule/class.php','module_description'); }

        function hasContent() { return true; }
        function hasSources() { return true; }
        function hasViews()   { return true; }
        function getRSSType() { return "rsnews"; }

        function supportsWorkflow() { return true; }

        function permissions($internal = '') {
            $i18n = exponent_lang_loadFile('modules/rsnewsmodule/class.php');
            if ($internal == '') {
                return array(
                'administrate'=>$i18n['perm_administrate'],
                'configure'=>$i18n['perm_configure'],
                'add_item'=>$i18n['perm_add_item'],
                'delete_item'=>$i18n['perm_delete_item'],
                'edit_item'=>$i18n['perm_edit_item'],
                'view_unpublished'=>$i18n['perm_view_unpublished'],
                'approve'=>$i18n['perm_approve'],
                'manage_approval'=>$i18n['perm_manage_approval']
                );
            } else {
                return array(
                'administrate'=>$i18n['perm_administrate'],
                'delete_item'=>$i18n['perm_delete_item'],
                'edit_item'=>$i18n['perm_edit_item']
                );
            }
        }

        function getRSSContent($loc) {
            global $db;

            //Get this modules configuration data
            $config = $db->selectObject('rsnewsmodule_config',"location_data='".serialize($loc)."'");

            //If this module was configured as an aggregator, then turn off check for the location_data
            if (isset($config->aggregate) && $config->aggregate == true) {
                $ifloc = '';
            } else {
                $ifloc = "location_data='" . serialize($loc) . "' AND ";
            }

            //Get this modules items
            $items = array();
            //$items = $db->selectObjects("rsnewsitem", "location_data='".serialize($loc)."'");
            $items = $db->selectObjects('rsnewsitem',$ifloc."(publish = 0 or publish <= " . time() . ') AND (unpublish = 0 or unpublish > ' . time() . ') AND approved != 0 ORDER BY '.$config->sortfield.' ' . $config->sortorder);

            //Convert the rsnewsitems to rss items
            $rssitems = array();
            foreach ($items as $key => $item) {	
                $rss_item = new FeedItem();
                $rss_item->title = $item->title;
                $rss_item->description = $item->body;
                $rss_item->date = date('r',$item->posted);
                $rss_item->link = "http://".HOSTNAME.PATH_RELATIVE."index.php?module=rsnewsmodule&action=view&id=".$item->id."&src=".$loc->src;
                $rssitems[$key] = $rss_item;
            }
            return $rssitems;
        }

        function getLocationHierarchy($loc) {
            if ($loc->int == '') {
                return array($loc);
            } else {
                return array($loc,exponent_core_makeLocation($loc->mod,$loc->src));
            }
        }

        function deleteIn($location) {
            global $db;
            $refcount = $db->selectValue('sectionref', 'refcount', "source='".$loc->src."'");
            if ($refcount <= 0) {
                $items = $db->selectObjects('rsnewsitem',"location_data='".serialize($location)."'");
                foreach ($items as $i) {
                    $db->delete('rsnewsitem_wf_revision','wf_original='.$i->id);
                    $db->delete('rsnewsitem_wf_info','real_id='.$i->id);
                }
                $db->delete('rsnewsitem',"location_data='".serialize($location)."'");
            }
        }

        function copyContent($oloc,$nloc) {
            global $db;
            foreach ($db->selectObjects('rsnewsitem',"location_data='".serialize($oloc)."'") as $n) {
                $revs = $db->selectObjects('rsnewsitem_wf_revision','wf_original='.$n->id);

                $n->location_data = serialize($nloc);
                unset($n->id);
                $n->id = $db->insertObject($n,'rsnewsitem');

                foreach ($revs as $rev) {
                    unset($rev->id);
                    $rev->wf_original = $n->id;
                    $rev->location_data = serialize($nloc);
                    $db->insertObject($rev,'rsnewsitem_wf_revision');
                }
            }
        }
        
        function show($view,$loc = null,$title = '') {
            $_REQUEST['page']=1;
            //Oct 14, 2010 - immanuel192   - Fix bug flow set cause loop forever - begin
            $_REQUEST['flow_has_set']=1; // to compatipility with view_page
            // remove exponent flow set here 
            // exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_SECTIONAL);
            //Oct 14, 2010 - immanuel192   - Fix bug flow set cause loop forever - begin
            include("actions/view_page.php");
        }

        function searchName() {
            $i8 = exponent_lang_loadFile('modules/rsnewsmodule/class.php') ;
            return $i8['bds'];
        }

        function spiderContent($item = null) {
            global $db;

            $i18n = exponent_lang_loadFile('modules/rsnewsmodule/class.php');

            if (!defined('SYS_SEARCH')) include_once(BASE.'subsystems/search.php');

            $search = null;
            $search->category = $i18n['search_category'];
            $search->ref_module = 'rsnewsmodule';
            $search->ref_type = 'rsnewsitem';

            if ($item) {
                $db->delete('search',"ref_module='rsnewsmodule' AND ref_type='rsnewsitem' AND original_id=" . $item->id);
                $search->original_id = $item->id;
                $search->title = ' ' . $item->title . ' ';
                $param=null;
                $param['action']='view';
                $param['module']='rsnewsmodule';
                $param['id']=$item->internal_name;

                $search->view_link = exponent_core_makeLink($param);
                $search->view_link=substr($search->view_link, strlen(URL_FULL));
                $search->keywords=' '.$item->title.' ';
                $search->body = ' ' . exponent_search_removeHTML($item->body) . ' ';
                $search->location_data = $item->location_data;
                $db->insertObject($search,'search');
            } else {
                $db->delete('search',"ref_module='rsnewsmodule' AND ref_type='rsnewsitem'");
                foreach ($db->selectObjects('rsnewsitem') as $item) {
                    $search->original_id = $item->id;
                    $search->title = ' ' . $item->title . ' ';
                    $param=null;
                    $param['action']='view';
                    $param['module']='rsnewsmodule';
                    $param['id']=$item->internal_name;

                    $search->view_link = exponent_core_makeLink($param);
                    $search->view_link=substr($search->view_link,strlen(URL_FULL));
                    $search->keywords=' '.$item->title.' ';
                    $search->body = ' ' . exponent_search_removeHTML($item->body) . ' ';
                    $search->location_data = $item->location_data;
                    $db->insertObject($search,'search');
                }
            }

            return true;
        }

    }

?>
